<?php
/**
 *      [迷你云] (C)2009-2012 南京恒为网络科技.
 *   软件仅供研究与学习使用，如需商用，请访问www.miniyun.cn获得授权
 * 
 */
?>
<?php

class AppListDataProvider extends CMiniyunActiveDataProvider
{
	private $_list;
	
	private function _fillUserMeta($key,&$item,$userMetaData){
		foreach($userMetaData as $index=>$userMetaItem){
			$meta_key = $userMetaItem->meta_key;
			$meta_value = $userMetaItem->meta_value;
			$user_id = $userMetaItem->user_id;
			if($user_id==$key){
				if($meta_key=="avatar"){
					$item->avatar=$meta_value;
				}
				if($meta_key=="email"){
					$item->email=$meta_value;
				}
				if($meta_key=="nick"){
					$item->nick=$meta_value;
				}
				if($meta_key=="phone"){
					$item->phone=$meta_value;
				}
				if($meta_key=="space"){
					$item->space=(int)$meta_value;
				}
				if($meta_key=="is_admin"){
					$item->isAdmin = $meta_value=="1"?true:false;
				}
			}
		}
				if(!isset($item->avatar)){
			$item->avatar=Yii::app()->params["defaultAvatar"];
		}
		if(!isset($item->email)){
			$item->email="";
		}
		if(!isset($item->phone)){
			$item->phone="";
		}
		if(!isset($item->isAdmin)){
			$item->isAdmin=false;
		}
		if(!isset($item->space)){
			$item->space=Yii::app()->params["app"]["defaultSpace"];
		}
	}
	
	private function _fillUsedSpace($key,&$item,$usedSpaceData){
		$fileCount = 0;
		foreach($usedSpaceData as $index=>$usedSpaceItem){
			$user_id = $usedSpaceItem["user_id"];
			if($user_id==$key){
				$item->usedSpace = $usedSpaceItem["usedSpace"]/(1024*1024);			}
		}
		if(!isset($item->usedSpace)){
			$item->usedSpace = 0;
		}
	}

	
	public function getData($refresh=false){
		if($this->_list===null || $refresh){
			$this->model = new ServerRegistry;			$this->initData("osr_usa_id_ref != -1", "osr_issue_date desc");			$ids = $this->ids;
			$data = array();
			foreach($this->data as $index=>$appItem){
				$item = new AppBase();
				$item->id=$appItem["osr_id"];
				$item->consumerKey=$appItem["osr_consumer_key"];
				$item->consumerSecret=$appItem["osr_consumer_secret"];
				$item->enabled=$appItem["osr_enabled"];
				$item->requesterName=$appItem["osr_requester_name"];
				$item->applicationDescr=$appItem["osr_application_descr"];
				array_push($data,$item);
			}
			$this->_list =  $data;
		}
		return $this->_list;
	}


}